// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.NavOrderRule;
import com.fowo.api.model.nav.order.rule.NavOrderRuleImportPo;
import com.fowo.api.model.nav.order.rule.NavOrderRuleItemVo;
import com.fowo.api.model.nav.order.rule.NavOrderRuleSearchParamPo;
import com.fowo.api.model.nav.order.rule.NavOrderRuleVo;
import com.fowo.api.service.NavOrderRuleService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "自发货订单规则")
@RequestMapping("/api/nav-order-rule")
@RestController
public class NavOrderRuleController {

  @Resource
  private NavOrderRuleService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<NavOrderRuleItemVo>> search(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<NavOrderRuleItemVo>>> searchOptions(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<NavOrderRuleItemVo>> getPage(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<NavOrderRuleVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取规则名称相关的编号列表")
  @GetMapping("name/{texts}/ids")
  public R<List<Long>> getIdsByName(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByName(nameList));
  }

  @Operation(summary = "获取编号相关的规则名称列表")
  @GetMapping("name/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getNameByIds(idList));
  }

  @PreAuthorize("@am.canAccess('navOrderRuleCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody NavOrderRuleVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('navOrderRuleEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody NavOrderRuleVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('navOrderRuleEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody NavOrderRuleVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('navOrderRuleDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('navOrderRuleDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('navOrderRuleImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<NavOrderRuleImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('navOrderRuleImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<NavOrderRuleImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(
      response,
      "自发货订单规则导入模版.xlsx"
    );
    InputStream inputStream = new ClassPathResource(
      "templates/NavOrderRule.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('navOrderRuleExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    NavOrderRuleSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('navOrderRuleCopy')")
  @Operation(summary = "拷贝")
  @PostMapping("copy")
  public R copy(@RequestBody IdPo po) {
    return R.run(() -> service.copy(po.getId()));
  }

  @PreAuthorize("@am.canAccess('navOrderRuleEnable')")
  @Operation(summary = "启用")
  @PostMapping("enable")
  public R enable(@RequestBody IdListPo po) {
    return R.run(() -> service.enable(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('navOrderRuleDisable')")
  @Operation(summary = "禁用")
  @PostMapping("disable")
  public R disable(@RequestBody IdListPo po) {
    return R.run(() -> service.disable(po.getIds()));
  }

  /**
   * 视图选项卡标题上显示计数
   */
  @Operation(summary = "视图选项卡标题上显示计数")
  @GetMapping("getTabHeaderShowTotal")
  public R<Map> getTabHeaderShowTotal() {
    return R.data(() -> service.getTabHeaderShowTotal());
  }

  @Operation(summary = "拆分订单")
  @GetMapping("split")
  public R<List<NavOrderRuleItemVo>> split(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.split(search));
  }

  @Operation(summary = "拆分订单分页")
  @GetMapping("splitPage")
  public R<Page<NavOrderRuleItemVo>> splitPage(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.splitPage(search));
  }

  @Operation(summary = "拆分订单最后更新时间")
  @GetMapping("splitLastUpdateTime")
  public R<Date> splitLastUpdateTime(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.splitLastUpdateTime(search));
  }

  @Operation(summary = "合并订单")
  @GetMapping("join")
  public R<List<NavOrderRuleItemVo>> join(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.join(search));
  }

  @Operation(summary = "合并订单分页")
  @GetMapping("joinPage")
  public R<Page<NavOrderRuleItemVo>> joinPage(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.joinPage(search));
  }

  @Operation(summary = "合并订单最后更新时间")
  @GetMapping("joinLastUpdateTime")
  public R<Date> joinLastUpdateTime(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.joinLastUpdateTime(search));
  }

  @Operation(summary = "设定仓库")
  @GetMapping("warehouse")
  public R<List<NavOrderRuleItemVo>> warehouse(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.warehouse(search));
  }

  @Operation(summary = "设定仓库分页")
  @GetMapping("warehousePage")
  public R<Page<NavOrderRuleItemVo>> warehousePage(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.warehousePage(search));
  }

  @Operation(summary = "设定仓库最后更新时间")
  @GetMapping("warehouseLastUpdateTime")
  public R<Date> warehouseLastUpdateTime(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.warehouseLastUpdateTime(search));
  }

  @Operation(summary = "设定物流")
  @GetMapping("logistics")
  public R<List<NavOrderRuleItemVo>> logistics(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.logistics(search));
  }

  @Operation(summary = "设定物流分页")
  @GetMapping("logisticsPage")
  public R<Page<NavOrderRuleItemVo>> logisticsPage(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.logisticsPage(search));
  }

  @Operation(summary = "设定物流最后更新时间")
  @GetMapping("logisticsLastUpdateTime")
  public R<Date> logisticsLastUpdateTime(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.logisticsLastUpdateTime(search));
  }

  @Operation(summary = "配置地址")
  @GetMapping("address")
  public R<List<NavOrderRuleItemVo>> address(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.address(search));
  }

  @Operation(summary = "配置地址分页")
  @GetMapping("addressPage")
  public R<Page<NavOrderRuleItemVo>> addressPage(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.addressPage(search));
  }

  @Operation(summary = "配置地址最后更新时间")
  @GetMapping("addressLastUpdateTime")
  public R<Date> addressLastUpdateTime(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.addressLastUpdateTime(search));
  }

  @Operation(summary = "订单审核")
  @GetMapping("process")
  public R<List<NavOrderRuleItemVo>> process(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.process(search));
  }

  @Operation(summary = "订单审核分页")
  @GetMapping("processPage")
  public R<Page<NavOrderRuleItemVo>> processPage(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processPage(search));
  }

  @Operation(summary = "订单审核最后更新时间")
  @GetMapping("processLastUpdateTime")
  public R<Date> processLastUpdateTime(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processLastUpdateTime(search));
  }

  @Operation(summary = "订单标发")
  @GetMapping("issu")
  public R<List<NavOrderRuleItemVo>> issu(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.issu(search));
  }

  @Operation(summary = "订单标发分页")
  @GetMapping("issuPage")
  public R<Page<NavOrderRuleItemVo>> issuPage(
    NavOrderRuleSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.issuPage(search));
  }

  @Operation(summary = "订单标发最后更新时间")
  @GetMapping("issuLastUpdateTime")
  public R<Date> issuLastUpdateTime(NavOrderRuleSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.issuLastUpdateTime(search));
  }
}
